/**
 @Name：系统管理
 @Author：cike
 @Date：2019-5-26
**/

layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,setter = layui.setter
  ,form = layui.form;

  // table设置
  table.set({
    where: {
      access_token: layui.data(setter.tableName)[setter.request.tokenName]
    }
    ,method: 'post'
    ,response:{
      statusCode: 200
    }
    ,page: true
    ,text: { none : "暂无相关数据" }
    ,done: function (res) {
      if(res[setter.response.statusCode] == setter.response.statusCode.logout){
        view.exit();
      }
    }
    ,parseData: function(res){ //res 即为原始返回的数据
      return {
        "code": res.code, //解析接口状态
        "msg": res.msg, //解析提示文本
        "count": res.data.total, //解析数据长度
        "data": res.data.list //解析数据列表
      };
    }
  });
  
  //系统管理
  table.render({
    elem: '#back-config-list'
    ,url: setter.api_back_config_list
    ,cols: [[
      {field: 'id', width: 80, title: 'ID',sort: true}
      ,{field: 'sort', width: 80, title: '排序', sort: true, edit:'text'}
      ,{field: 'name', width: 200, title: '名称'}
      ,{field: 'alias', width: 200, title: '别名'}
      ,{field: 'type', width: 100, title: '类型'}
      ,{field: 'value', title: '值'}
      ,{field: 'desc', title: '描述'}
      ,{field: 'update_time', title: '更新时间'}
      ,{field: 'create_time', title: '创建时间'}
      ,{title: '操作', width: 220, align: 'center', toolbar: '#back-config-list-operation'}
    ]]
  });
  

  //监听工具条
  table.on('edit(back-config-list)', function(obj){ 
    admin.req({
      url: setter.api_back_config_update
      ,data: obj.data
      ,type: 'post'
      ,done: function(res){
        layer.msg(res.msg);
        layui.table.reload('back-config-list'); //重载表格
      }
    });
  });

  table.on('tool(back-config-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此配置？', function(index){
        admin.req({
          url: setter.api_back_config_delete
          ,data: {id:obj.data.id}
          ,type: 'post'
          ,done: function(res){
            layer.msg(res.msg);
            layui.table.reload('back-config-list'); //重载表格
          }
        });
        layer.close(index); //执行关闭 
      });
    }else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑配置'
        ,area: admin.screen() < 2 ? ['100%','530px'] : ['450px','530px']
        ,id: 'back-config-edit-popup'
        ,success: function(layero, index){
          view(this.id).render('config/form', data).done(function(){
            form.render(null, 'back-config-form');
            //监听提交
            form.on('submit(back-config-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              admin.req({
                url: setter.api_back_config_update
                ,data: field
                ,type: 'post'
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭
                  layui.table.reload('back-config-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
            });
          });
        }
      });
    }
  });

  exports('website', {})
});